# 导入view的json对象
import json

# 导入django的响应对象
from django.shortcuts import HttpResponse

# 导入barChart的 barBase
from DataChart.chart.barChart import barBase, pieBase, lineBase, mapBase
# 导入dao包下的search函数
from DataChart.dao.loginDao import search
from DataChart.view import JsonResponse


# 定义请求响应函数
def barChart(request):
    return JsonResponse(json.loads(barBase()))


def lineChart(request):
    return JsonResponse(json.loads(lineBase()))


def pieChart(request):
    return JsonResponse(json.loads(pieBase()))


def mapChart(request):
    return JsonResponse(json.loads(mapBase()))


# 登录请求
def login(request):
    # 获取用户名
    name = request.GET.get("userName")
    # 获取密码
    pwd = request.GET.get("userPwd")
    print("name=", name)
    print("pwd=", pwd)
    rs = search(name, pwd)
    if len(rs) > 0:
        # 返回响应信息
        info = {"info": "登录成功", "code": 200}
    else:
        info = {"info": "登录失败", "code": 500}

    return HttpResponse(json.dumps(info), content_type="application/json")
